import os
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, Settings
from llama_index.llms.dashscope import DashScope
from llama_index.embeddings.dashscope import DashScopeEmbedding
# 加载环境变量
from dotenv import load_dotenv
load_dotenv()
llm_model = os.getenv("DASHSCOPE_LLM_MODEL")
llm_api_key = os.getenv("DASHSCOPE_API_KEY")
embedding_model = os.getenv("DASHSCOPE_EMBEDDING_MODEL", "text-embedding-v1")

print(f"llm_model: {llm_model}")
print(f"llm_api_key: {llm_api_key}")
print(f"embedding_model: {embedding_model}")
Settings.llm  = DashScope(model_name=llm_model, api_key=llm_api_key)
# 创建 embedding 实例并手动设置私有属性
embedding = DashScopeEmbedding(model_name=embedding_model, api_key=llm_api_key)
# 手动设置私有属性
embedding._api_key = llm_api_key
embedding._text_type = "document"
Settings.embed_model = embedding


def load_data(data_path):
    reader = SimpleDirectoryReader(data_path)
    docs = reader.load_data()
    return docs

def build_index(docs):
    print("开始构建索引")
    index = VectorStoreIndex.from_documents(docs)
    index.storage_context.persist("./index")
    return index




if __name__ == "__main__":
    print("开始运行day 01")
    docs = load_data("./data")
    print(f"加载数据完成，共{len(docs)}条数据")
    index = build_index(docs)
    print("索引构建完成")







    